<template>
  <div style="margin-top:30px">
    <!-- 七分钟 -->
    <el-form ref="form" :model="courseInfo" label-width="100px">
      <el-form-item label="课程标题">
        <el-input v-model="courseInfo.title"/>
      </el-form-item>
      <el-form-item label="课程讲师">
        <el-select v-model="courseInfo.teacherId" placeholder="请选择讲师">
          <el-option v-for="teacher in teachers" :key="teacher.id" :label="teacher.name" :value="teacher.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="课程类别">
        <el-select v-model="courseInfo.subjectParentId" placeholder="请选择一级课程" @change="handlePSubjectChanged">
          <el-option v-for="pSubject in levelOneSubjects" :key="pSubject.id" :label="pSubject.title" :value="pSubject.id"/>
        </el-select>
        <el-select v-model="courseInfo.subjectId" placeholder="请选择二级课程">
          <el-option v-for="cSubject in levelTwoSubjects" :key="cSubject.id" :label="cSubject.title" :value="cSubject.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="总课时">
        <el-input-number v-model="courseInfo.lessonNum" :min="1" controls-position="right"/>
      </el-form-item>
      <el-form-item label="课程简介">
        <tinymce v-model="courseInfo.description">
        <!-- <el-select v-model="courseInfo.description" aria-placeholder="请选择活动区域"/> -->
      </tinymce></el-form-item>
      <el-form-item label="课程封面">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="uploadUrl"
          class="avatar-uploader">
          <img v-if="courseInfo.cover" :src="courseInfo.cover" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"/>
        </el-upload>

      </el-form-item>
      <el-form-item label="课程价格">
        <el-input-number v-model="courseInfo.price" :min="0" controls-position="right"/>元
      </el-form-item>
    </el-form>

    <div style="text-aglin：center">
      <el-button :disabled="btnDisabled" type="primary" @click="saveAndNext">
        保存并下一步
      </el-button>
    </div>
  </div>
</template>
<script>
import teacherApi from '@/api/teacher'
import subjectApi from '@/api/subject'
import Tinymce from '@/components/Tinymce'
import courseApi from '@/api/course'
export default {
  components: {
    Tinymce
  },
  data() {
    return {
      courseInfo: {
        subjectId: '',
        price: 0,
        lessonNum: 1
      },
      btnDisabled: false,
      teachers: [],
      uploadUrl: 'http://localhost:8120/admin/oss/file/upload?module=cover',
      levelOneSubjects: [],
      levelTwoSubjects: []
    }
  },
  created() {
    teacherApi.getTeachers().then(r => {
      this.teachers = r.data.items
    })
    subjectApi.getSubjectNestedList().then(r => {
      this.levelOneSubjects = r.data.items

      if (this.$route.params.id) {
        this.$parent.courseId = this.$route.params.id
      }

      if (this.$parent.courseId) {
        courseApi.getCourseInfo(this.$parent.courseId).then(r => {
          this.courseInfo = r.data.item

          this.levelOneSubjects.forEach(pSubject => {
            if (pSubject.id === this.courseInfo.subjectPatentId) {
              this.levelTwoSubjects = pSubject.children
            }
          })
        })
      }
    })
  },
  methods: {
    save() {
      // console.log(this.courseInfo)
      courseApi.saveCourseInfo(this.courseInfo).then(r => {
        this.$message.success('新增成功')
        this.$parent.courseId = r.data.id
        this.$parent.active = 1
      })
    },
    update() {
      courseApi.updateCourseInfo(this.courseInfo, this.$parent.courseId).then(r => {
        this.$message.success('更新成功')
        // this.$parent.courseId = r.data.id
        this.$parent.active = 1
      })
    },
    handlePSubjectChanged(psubjectId) {
      // console.log(psubjectId)
      this.courseInfo.subjectId = ''
      this.levelOneSubjects.forEach(pSubject => {
        if (pSubject.id === psubjectId) {
          this.levelTwoSubjects = pSubject.children
        }
      })
    },
    saveAndNext() {
      this.btnDisabled = true
      if (this.$parent.courseId) {
        // 更新
        this.update()
      } else {
        this.save()
      }
    },
    handleAvatarSuccess(res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw)
      this.courseInfo.cover = res.data.path
      this.$forceUpdate()
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>

<style>
  .tinymce-container {
    position: relative;
    line-height: normal;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 356px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 356px;
    height: 178px;
    display: block;
  }
</style>
